/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.listener.processor;

import org.apache.axiom.om.*;
import org.apache.axiom.soap.*;
import org.apache.log4j.*;

import javax.xml.namespace.*;

import lk.mrt.cse.wihidum.listener.storage.*;
import lk.mrt.cse.wihidum.util.WSEventingConstants;   
/**
 * Process store publication messages
 */
public class StorePublicationProcessor
        implements Processor
{
    private static final Logger logger = Logger.getLogger (StorePublicationProcessor.class);

    public OMElement processMessage (SOAPEnvelope requestElement)
    {
        try
        {
            logger.info ("Processing store publication message");

            SOAPHeader header = requestElement.getHeader();
            SOAPBody body = requestElement.getBody();

            OMElement publicationDeliveryHeaderElement = header.getFirstChildWithName (new QName (WSEventingConstants.WIHIDUM_NAMESPACE, "PublicationDeliveryHeader"));

            OMElement subscriptionIDElement = publicationDeliveryHeaderElement.getFirstChildWithName (new QName (WSEventingConstants.WIHIDUM_NAMESPACE, "SubscriptionID"));
            OMElement publisherAddressElement = publicationDeliveryHeaderElement.getFirstChildWithName (new QName (WSEventingConstants.WIHIDUM_NAMESPACE, "PublisherAddress"));
            OMElement topicElement = publicationDeliveryHeaderElement.getFirstChildWithName (new QName (WSEventingConstants.WIHIDUM_NAMESPACE, "Topic"));
         
            DeliveryElement deliveryElement = new DeliveryElement ();      

            deliveryElement.setSubscriptionID (subscriptionIDElement.getText ());
            deliveryElement.setPublisherService (publisherAddressElement.getText ());
            deliveryElement.setTopic (topicElement.getText ());
            deliveryElement.setPublicationText (body.getText ());

            DeliveryElementManager deliveryElementManager = new DeliveryElementDAO ();
            deliveryElementManager.saveDeliveryElement (deliveryElement);

            String response = "Store publication completed successfully";
            return createPublicationResponseElement (response);
        }
        catch (OMException ex)
        {
            logger.error ("Error in processing store publication message", ex);
            return createPublicationFaultResponseElement ();
        }
        catch (NullPointerException ex)
        {
            logger.error ("Error in processing store publication message", ex);
            return createPublicationFaultResponseElement ();
        }
    }

    /**
     * Create response for a valid store publication
     *
     * @param response Response message
     * @return Response element
     */
    private OMElement createPublicationResponseElement (String response)
    {
        SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory ();

        OMNamespace wihidumNS = soapFactory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE, WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
        OMElement responseElement = soapFactory.createOMElement ("StorePublicationResponse", wihidumNS);

        responseElement.setText (response);
        return responseElement;
    }

    /**
     * Create response for an invalid store publication
     *
     * @return Response element
     */
    private OMElement createPublicationFaultResponseElement ()
    {
        return null;
    }
}